<template>
    <div>
      <!-- 搜索结果页-头部导航 -->
      <div class="search-result-container">
        <!-- 点击实现后退效果 -->
        <van-nav-bar
          title="搜索结果"
          left-arrow
          @click-left="$router.go(-1)"
          fixed
        />
      </div>
      <van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  offset='50'
  :immediate-check='false'
  @load="onLoad"
>
  <article-item v-for='item in articleList' :key='item.art_id' :Obj='item'></article-item>
    </van-list>
    </div>
</template>

<script>
import { timeAgo } from '@/utils/date.js'
import ArticleItem from '@/components/ArticleItem.vue'
import { searchResultListAPI } from '@/api'
export default {
  components: {
    ArticleItem
  },
  data () {
    return {
      articleList: [],
      loading: false, // 加载状态
      finished: false, // 全部加载完成状态
      page: 1
    }
  },
  name: 'SearchResult',
  async create () {
    this.getArticleList()
    // $route 当前路由的对象 对象提供了当前页面路由的所有路由参数
    // $router 路由的实例 里面提供了路由方法， 路由钩子, 完整的路由表, currentRout当前页面的路由对象
    // $router中的currentRoute就相当于$route
    // console.log(this.$route)
    // console.log(this.$router)
  },
  methods: {
    async getArticleList () {
      this.page++
      const res = await searchResultListAPI({
        q: this.$route.params.keywords, // 拿到关键词, keywords来源于router/index.js你定义的动态路由参数名
        page: this.page

      })
      console.log(res)
      // 数据处理
      res.data.data.results.forEach(item => {
        item.pubdate = timeAgo(item.pubdate)
      })
      if (res.data.data.results.length === 0) {
        this.finished = true
      } else {
        this.articleList = [...this.articleList, ...res.data.data.results]
        this.loading = false
      }
    },
    onLoad () {
      // console.log('触发底部加载')
      if (this.articleList.length > 0) {
        this.page++
        this.getArticleList()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
